home *** CD-ROM | disk | FTP | other *** search
- //////////////////////////////////////////////////////////////////
- //
- // Binary.c
- // --------
- // Binary tree search and insertion
- //
- //////////////////////////////////////////////////////////////////
-
- #pragma load "managers"
-
- //////////////////////////////////////////////////////////////////
- //
- // Constants and Macros
- //
- //////////////////////////////////////////////////////////////////
-
- #define nil 0
-
- //////////////////////////////////////////////////////////////////
- //
- // Types
- //
- //////////////////////////////////////////////////////////////////
-
- typedef enum {false, true} logical;
-
- typedef struct node
- {
- char *key;
- struct node *left;
- struct node *right;
- char *data;
- } node;
-
- //////////////////////////////////////////////////////////////////
- //
- // Globals
- //
- //////////////////////////////////////////////////////////////////
-
- //////////////////////////////////////////////////////////////////
- //
- // Prototypes
- //
- //////////////////////////////////////////////////////////////////
-
- void initmac();
- node *createnode(char *thekey, char *thedata);
- node *insert(node *thetable, char *thekey, char *thedata);
- node *lookup(node *thetable, char *thekey);
- void dump(node *thetable);
- void destroy(node *thetable);
-
- //////////////////////////////////////////////////////////////////
- //
- // main
- // ----
- // Test shell for lookup table package
- //
- //////////////////////////////////////////////////////////////////
-
- main()
- {
-
- node *thetable;
- char thestring[256];
- char command;
- char thekey[256];
- char thedata[256];
- node *thenode;
-
- initmac();
-
- thetable = createnode("", "");
- if (thetable == nil)
- {
- printf("\tUnable to create table\n");
- exit(2);
- }
-
- printf("? ");
- while (true)
- {
-
- gets(thestring);
- sscanf(&thestring[2], "%c %s %[^\n]", &command, thekey, thedata);
-
- switch (command)
- {
-
- case 'A':
- case 'a':
- if (insert(thetable, thekey, thedata) == nil)
- printf("\tKey already exists in table\n");
- break;
-
- case 'D':
- case 'd':
- dump(thetable->right);
- break;
-
- case 'F':
- case 'f':
- thenode = lookup(thetable, thekey);
- if (thenode == nil)
- {
- printf("\tKey not found in table\n");
- break;
- }
- printf("\tData is “%s”\n", thenode->data);
- break;
-
- case 'Q':
- case 'q':
- destroy(thetable);
- exit(0);
-
- }
-
- printf("? ");
-
- }
-
- }
-
- //////////////////////////////////////////////////////////////////
- //
- // initmac
- // -------
- // initialize any necessary managers and whatnot.
- //
- //////////////////////////////////////////////////////////////////
-
- void initmac()
- {
-
- InitGraf((Ptr)&qd.thePort);
- SetFScaleDisable(true);
-
- InitCursorCtl(nil);
-
- }
-
- //////////////////////////////////////////////////////////////////
- //
- // createnode
- // ----------
- // create and initialize a new node
- //
- //////////////////////////////////////////////////////////////////
-
- node *createnode(char *thekey, char *thedata)
- {
-
- node *thenode;
- int thelength;
-
- thenode = (node *)NewPtr(sizeof(node));
- if (thenode == nil)
- return(nil);
-
- thelength = 1 + strlen(thekey);
- thenode->key = (char *)NewPtr(thelength);
- if (thenode->key == nil)
- {
- DisposPtr((Ptr)thenode);
- return(nil);
- }
- BlockMove((Ptr)thekey, (Ptr)thenode->key, thelength);
-
- thenode->left = nil;
- thenode->right = nil;
-
- thelength = 1 + strlen(thedata);
- thenode->data = (char *)NewPtr(thelength);
- if (thenode->data == nil)
- {
- DisposPtr((Ptr)thenode->key);
- DisposPtr((Ptr)thenode);
- return(nil);
- }
- BlockMove((Ptr)thedata, (Ptr)thenode->data, thelength);
-
- return(thenode);
-
- }
-
- //////////////////////////////////////////////////////////////////
- //
- // insert
- // ------
- // Insert a new key into the table
- //
- //////////////////////////////////////////////////////////////////
-
- node *insert(node *thetable, char *thekey, char *thedata)
- {
-
- node *thenode;
- int compare;
- node *thechild;
-
- thenode = thetable;
-
- while (compare = strcmp(thekey, thenode->key))
- {
-
- if (compare < 0)
- {
-
- thechild = thenode->left;
- if (thechild == nil)
- {
- thenode->left = createnode(thekey, thedata);
- return(thenode->left);
- }
-
- }
- else
- {
-
- thechild = thenode->right;
- if (thechild == nil)
- {
- thenode->right = createnode(thekey, thedata);
- return(thenode->right);
- }
-
- }
-
- thenode = thechild;
-
- }
-
- return(nil);
-
- }
-
- //////////////////////////////////////////////////////////////////
- //
- // lookup
- // ------
- // Find a key in the table
- //
- //////////////////////////////////////////////////////////////////
-
- node *lookup(node *thetable, char *thekey)
- {
-
- node *thenode;
- int compare;
-
- thenode = thetable;
-
- while (compare = strcmp(thekey, thenode->key))
- {
- if (compare < 0)
- thenode = thenode->left;
- else
- thenode = thenode->right;
- if (thenode == nil)
- return(nil);
- }
-
- return(thenode);
-
- }
-
- //////////////////////////////////////////////////////////////////
- //
- // dump
- // ----
- // Display the table, node by node
- //
- //////////////////////////////////////////////////////////////////
-
- void dump(node *thetable)
- {
-
- if (thetable == nil)
- return;
-
- dump(thetable->left);
-
- printf("%10s - %10s - %10s\n",
- thetable->key,
- thetable->left ? thetable->left->key : "nil",
- thetable->right ? thetable->right->key : "nil");
-
- dump(thetable->right);
-
- }
-
- //////////////////////////////////////////////////////////////////
- //
- // destroy
- // -------
- // Dispose of the table
- //
- //////////////////////////////////////////////////////////////////
-
- void destroy(node *thetable)
- {
-
- if (thetable == nil)
- return;
-
- destroy(thetable->left);
- destroy(thetable->right);
-
- DisposPtr((Ptr)thetable->key);
- DisposPtr((Ptr)thetable->data);
- DisposPtr((Ptr)thetable);
-
- }
-
- //////////////////////////////////////////////////////////////////
-